from django.shortcuts import render
from .models import Medical_record
import jwt
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import AllowAny
from rest_framework_simplejwt.tokens import RefreshToken
from patient.models import Patient
from doctor.models import Doctor
from appointment.models import Appointment
import os
from django.utils.timezone import now
from django.conf import settings


# Create your views here.


class Get_All(APIView):
    permission_classes = [AllowAny]  # 允许任何人访问

    def get(self, request):
        patient_id = request.GET.get('patient_id')

        if not patient_id:
            return Response({"error": "patient_id is required"}, status=400)

        try:
            # 查找 Patient 实例
            medical_records = Medical_record.objects.defer('content').filter(patient_id=patient_id)
            medical_records_data = list(
                medical_records.values('id', 'title', 'date', 'is_completed', 'patient_id', 'appointment'))
        except Medical_record.DoesNotExist:
            return Response({"error": "Patient not found"}, status=400)

        if medical_records:
            return Response({'medical_records': medical_records_data}, status=200)

        return Response({"message": "暂无 medical_records"}, status=200)


class Get_Target(APIView):
    permission_classes = [AllowAny]  # 允许任何人访问

    def get(self, request):
        medical_record_id = request.GET.get('medical_record_id')

        if not medical_record_id:
            return Response({"error": "medical_record_id is required"}, status=400)

        try:
            # 查找 Patient 实例
            medical_record = Medical_record.objects.get(id=medical_record_id)
        except Medical_record.DoesNotExist:
            return Response({"message": "暂无 medical_record"}, status=200)

        if medical_record:
            return Response({'medical_record_id': medical_record_id,
                             'medical_record_title': medical_record.title,
                             'medical_record_date': medical_record.date,
                             'medical_record_is_completed': medical_record.is_completed,
                             'medical_record_patient_id': medical_record.patient_id.id,
                             'medical_record_patient_name': medical_record.patient_id.name,
                             'medical_record_content': medical_record.content,
                             'appointment_id': medical_record.appointment.id
                             }, status=200)

        return Response({"error": "medical_record not found"}, status=400)


class Create(APIView):
    permission_classes = [AllowAny]  # 允许任何人访问

    def post(self, request):
        # medical_record_title = request.data.get('medical_record_title')
        # medical_record_content = request.data.get('medical_record_contend')
        medical_record_appointment_id = request.data.get('appointment_id')
        appointment_instance = Appointment.objects.get(id=medical_record_appointment_id)
        patient_instance = appointment_instance.patient
        medical_record_patient_id = patient_instance.id
        # medical_record_appointment_id = request.data.get('appointment_id')

        # if not medical_record_title:
        #     return Response({"error": "medical_record_title is required"}, status=400)
        # if not medical_record_content:
        #     return Response({"error": "medical_record_content is required"}, status=400)
        if not medical_record_patient_id:
            return Response({"error": "medical_record_patient_id is required"}, status=400)
        if not medical_record_appointment_id:
            return Response({"error": "appointment_id is required"}, status=400)
        if medical_record_patient_id and medical_record_appointment_id:
            medical_record = Medical_record(patient_id=patient_instance, appointment=appointment_instance)
            medical_record.save()
            return Response({"message": "创建成功"}, status=200)

        return Response({"error": "Unsuccess"}, status=400)


# class Create(APIView):
#     permission_classes = [AllowAny]  # 允许任何人访问
#
#     def post(self, request):
#         medical_record_title = "测试"
#         medical_record_content = """
#             <p>常见病历模板</p>
#             <table style="width: 50%; border-collapse: collapse;" border="1">
#             <tbody>
#             <tr>
#             <td style="width: 50%;">诊断结果</td>
#             <td style="width: 50%;">感冒</td>
#             </tr>
#             <tr>
#             <td style="width: 50%;">主要描述</td>
#             <td style="width: 50%;">发烧</td>
#             </tr>
#             </tbody>
#             </table>
#         """
#         medical_record_patient_id = 1
#
#         if not medical_record_title:
#             return Response({"error": "medical_record_title is required"}, status=400)
#         if not medical_record_content:
#             return Response({"error": "medical_record_content is required"}, status=400)
#         if not medical_record_patient_id:
#             return Response({"error": "medical_record_patient_id is required"}, status=400)
#         if medical_record_title and medical_record_content and medical_record_patient_id:
#             Medical_record.objects.create(title=medical_record_title,
#                                           content=medical_record_content,
#                                           patient_id=medical_record_patient_id)
#             return Response({"message": "创建成功"}, status=200)
#
#         return Response({"error": "Unsuccess"}, status=400)

class Change(APIView):
    permission_classes = [AllowAny]  # 允许任何人访问

    def post(self, request):
        medical_record_title = request.data.get('medical_record_title')
        medical_record_id = request.data.get('medical_record_id')
        medical_record_content = request.data.get('medical_record_content')
        # medical_record_is_completed = request.data.get('medical_record_is_completed')

        if not medical_record_id:
            return Response({"error": "medical_record_id is required"}, status=400)
        if not medical_record_title:
            return Response({"error": "medical_record_title is required"}, status=400)
        if not medical_record_content:
            return Response({"error": "medical_record_content is required"}, status=400)
        # if not medical_record_is_completed:
        #     return Response({"error": "medical_record_is_completed is required"}, status=400)

        try:

            medical_record = Medical_record.objects.get(id=medical_record_id)
        except Medical_record.DoesNotExist:
            return Response({"error": "Medical_record not found"}, status=500)
        if medical_record_id and medical_record_content and medical_record_title:
            medical_record.title = medical_record_title
            medical_record.content = medical_record_content
            # medical_record.is_completed = medical_record_is_completed
            medical_record.save()

            return Response({"message": "Success"}, status=200)
        return Response({"error": "Unsuccess"}, status=500)


class Done(APIView):
    permission_classes = [AllowAny]  # 允许任何人访问

    def post(self, request):

        medical_record_id = request.data.get('medical_record_id')
        medical_record_is_completed = request.data.get('medical_record_is_completed')

        if not medical_record_id:
            return Response({"error": "medical_record_id is required"}, status=400)

        if not medical_record_is_completed:
            return Response({"error": "medical_record_is_completed is required"}, status=400)

        try:

            medical_record = Medical_record.objects.get(id=medical_record_id)
            appointment = Appointment.objects.get(id=medical_record.appointment.id)
        except Medical_record.DoesNotExist:
            return Response({"error": "Medical_record not found"}, status=500)
        if medical_record_id and medical_record_is_completed:
            medical_record.is_completed = medical_record_is_completed
            appointment.clinc_status = "Diagnosed"
            medical_record.save()
            appointment.save()

            return Response({"message": "Success"}, status=200)
        return Response({"error": "Unsuccess"}, status=500)
